<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/2/7
 * Time: 23:41
 */
declare(strict_types=1);

namespace App\Service;
use App\Utils\CustomUtils;
use App\Utils\RedisUtils;
use App\Model\Dao\MchInfoDao;
use App\Model\Dao\EventOrderDao;
use Swlib\SaberGM;
use Hyperf\Di\Annotation\Inject;

class MchAccountAccessService extends CommonService
{

    /**
     * @Inject()
     * @var MchInfoDao
     */
    private $MchInfoDao;

    /**
     * @Inject()
     * @var EventOrderDao
     */
    private $EventOrderDao;        

    /**
     * @Inject()
     * @var SettleTradeOrderService
     */
    private $SettleTradeOrderService;

    /**
     * @Inject()
     * @var SettleTradeOrderRefundService
     */
    private $SettleTradeOrderRefundService;

    /**
     * @Inject()
     * @var SettleTradeOrderDisLookIntoService
     */
    private $SettleTradeOrderDisLookIntoService;

    /**
     * @Inject()
     * @var SettleTradeOrderFrozenService
     */
    private $SettleTradeOrderFrozenService;

    /**
     * @Inject()
     * @var SettleTradeOrderThawService
     */
    private $SettleTradeOrderThawService;

    /**
     * @Inject()
     * @var SettleTradeFailService
     */
    private $SettleTradeFailService;

    /**
     * @Inject()
     * @var SettleTradeOrderThawBondService
     */
    private $SettleTradeOrderThawBondService;

    /**
     * @Inject()
     * @var SettleEventOrderService
     */
    private $SettleEventOrderService;

    /**
     * @Inject()
     * @var SettleWithdrawService
     */
    private $SettleWithdrawService;

    /**
     * @Inject()
     * @var SettleTransService
     */
    private $SettleTransService;

    /**
     * @Inject()
     * @var SettleTradeOrderDisFeeService
     */
    private $SettleTradeOrderDisFeeService;

    /**
     * @Inject()
     * @var SettleCommissionWithdrawService
     */
    private $SettleCommissionWithdrawService;

    /**
     * @Inject()
     * @var SettleTradeOrderForceSuccessService
     */
    private $SettleTradeOrderForceSuccessService;

    private function sendEmail($param,$emailType){
        $mchInfo = $this->MchInfoDao->getWhere(['mchNo' => $param['mchNo']]);

        $sendMailParam = array(
            'type'    => $emailType,
            'email'   => $mchInfo['email'],
            "mchNo"   => $param['mchNo'],
            // "tradeNo" => $param['tradeNo'],
        );
        switch($emailType)
        {
            case 3:
            case 4:
            case 5:
            case 6:
                $sendMailParam['tradeNo'] = $param['tradeNo'];
                break;
            case 7:
            case 8:
                $sendMailParam['transNo'] = $param['transNo'];
                break;
            default:
                

                // $sendMailParam['tradeNo'] = $param['tradeNo'];
                break;
        }
        CustomUtils::xzxDebug($sendMailParam);
        // $this->EmailService->addMq($sendMailParam);  
        // $result = null;

        try {

            $api = env('BZ_URL').'/admin/api/email/addMq';

            $result = SaberGM::post($api, $sendMailParam, ['timeout' => '20'])->getBody()->getContents();

        } catch (\Throwable $e) {
            CustomUtils::xzxDebug($e);
        }

        // if (!$result) {
        //     throw new \Exception('请求调用风控模块失败');
        // }

        // $result = json_decode($result, true);

    }

    /**
     * @param $param
     * handleType:
     * 交易类型
     * 1001.交易成功结算 2001.交易退款结算 3001.交易拒付结算 4001.交易冻结结算 5001.交易解冻结算 6001.交易失败 7001.保证金解冻 8001.事件订单
     * 格式： { "accessType": 1, "mchAccountId": "", "orderId": "", "refundId": "", "disId": "", frozenId: "", thawId: "",
     *          "eventOrderId": "", "handleStatus": 1, "accountType": 1, "amount": 100, "tryTimes": 1}
     * @return array|bool|null
     * @throws \Exception
     */
    public function handle($param) {
        $result = null;
        switch ($param['accessType']){
            // 交易成功结算
            case ACCESS_TYPE_TRADE_ORDER:
                $result = $this->SettleTradeOrderService->handle($param);
                break;
            // 交易退款结算
            case ACCESS_TYPE_TRADE_ORDER_REFUND:
                $result = $this->SettleTradeOrderRefundService->handle($param);
                if($result['code'] == SUCCESS_CODE && $param['handleStatus'] == 3){
                    // send email
                    $this->sendEmail($param,3);
                }
                break;                
            // 交易拒付调查结算
            case ACCESS_TYPE_TRADE_ORDER_DIS_LOOK_INTO:
                $result = $this->SettleTradeOrderDisLookIntoService->handle($param);
                if($result['code'] == SUCCESS_CODE && $param['handleStatus'] == 3){
                    // send email
                    $this->sendEmail($param,4);
                }
                break; 
            // 交易冻结结算
            case ACCESS_TYPE_TRADE_ORDER_FROZEN:
                $result = $this->SettleTradeOrderFrozenService->handle($param);
                if($result['code'] == SUCCESS_CODE && $param['handleStatus'] == 3){
                    // send email
                    $this->sendEmail($param,5);
                }                
                break;
            // 交易解冻结算
            case ACCESS_TYPE_TRADE_ORDER_THAW:
                $result = $this->SettleTradeOrderThawService->handle($param);
                if($result['code'] == SUCCESS_CODE && $param['handleStatus'] == 3){
                    // send email
                    $this->sendEmail($param,6);
                }                 
                break;
            // 交易失败结算
            case ACCESS_TYPE_TRADE_FAIL:
                $result = $this->SettleTradeFailService->handle($param);
                break;
            // 保证金解冻
            case ACCESS_TYPE_BOND_THAW:
                $result = $this->SettleTradeOrderThawBondService->handle($param);
                break;
            // 事件订单，这里给进来都给8001，根据db记录来处理实际的类型
            case ACCESS_TYPE_EVENT_ORDER_FROZEN:
                $result = $this->SettleEventOrderService->handle($param);
                // CustomUtils::xzxDebug($result);

                if($result['code'] == SUCCESS_CODE && $param['handleStatus'] == 3){
                    // send email
                    $eventOrder = $this->EventOrderDao->get($param['eventOrderId']);
                    $param['mchNo'] = $eventOrder['mchNo'];
                    switch($eventOrder['type'])
                    {
                        case 8001:                           
                            $this->sendEmail($param,9);
                            break;
                        case 8002:
                            $this->sendEmail($param,11);
                            break;
                        case 8003:
                            $this->sendEmail($param,12);
                            break;
                        case 8004:
                            $this->sendEmail($param,10);
                            break;
                    }
                    
                }
                break;
            // 提现
            case ACCESS_TYPE_WITHDRAW:
                $result = $this->SettleWithdrawService->handle($param);
                if($result['code'] == SUCCESS_CODE && $param['handleStatus'] == 3){
                    // send email
                    $this->sendEmail($param,7);
                }                  
                break;
            // 转账
            case ACCESS_TYPE_TRANS:
                $result = $this->SettleTransService->handle($param);
                if($result['code'] == SUCCESS_CODE && $param['handleStatus'] == 3){
                    // send email
                    $this->sendEmail($param,8);
                }                  
                break;
            // 拒付处理费
            case ACCESS_TYPE_DIS_FEE:
                $result = $this->SettleTradeOrderDisFeeService->handle($param);
                break;
            // 佣金提现单
            case ACCESS_TYPE_COMMISSION_WITHDRAW:
                $result = $this->SettleCommissionWithdrawService->handle($param);
                break;
            // 补单
            case ACCESS_TYPE_TRADE_ORDER_FORCE_SUCCESS:
                $result = $this->SettleTradeOrderForceSuccessService->handle($param);
                break;
            default:
                $result = CustomUtils::xeServiceReturnData(FAILED_CODE, "错误的处理类型");
                break;
        }
        return $result;
    }

}